<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0"><title>1.2 xpath简单介绍 | 梧桐的学习日记</title><meta name="author" content="梧桐影下月光卧"><meta name="copyright" content="梧桐影下月光卧"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="xpath解析: 最常用且便捷高效的一种解析方式 使用快捷方法：打开F12，右键点击html节点复制xpath  xpath解析原理:1. 实例化一个etree对象,将要解析的页面元am加载到该对象中 2. 调用etree对象中的xpath方法,结合xpath表达式实现标签定位和内容捕获  流程 安装 pip install lxml 导入 from lxml import etree 实例化et">
<meta property="og:type" content="article">
<meta property="og:title" content="1.2 xpath简单介绍">
<meta property="og:url" content="https://acwy.fun/2023/04/10/xpath/index.html">
<meta property="og:site_name" content="梧桐的学习日记">
<meta property="og:description" content="xpath解析: 最常用且便捷高效的一种解析方式 使用快捷方法：打开F12，右键点击html节点复制xpath  xpath解析原理:1. 实例化一个etree对象,将要解析的页面元am加载到该对象中 2. 调用etree对象中的xpath方法,结合xpath表达式实现标签定位和内容捕获  流程 安装 pip install lxml 导入 from lxml import etree 实例化et">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://www.w3cschool.cn/attachments/image/20161103/1478161528585228.png">
<meta property="article:published_time" content="2023-04-10T07:54:23.000Z">
<meta property="article:modified_time" content="2023-11-02T10:40:09.707Z">
<meta property="article:author" content="梧桐影下月光卧">
<meta property="article:tag" content="python">
<meta property="article:tag" content="自动化">
<meta property="article:tag" content="爬虫">
<meta property="article:tag" content="网页解析">
<meta property="article:tag" content="xpath">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://www.w3cschool.cn/attachments/image/20161103/1478161528585228.png"><link rel="shortcut icon" href="/img/logo.png"><link rel="canonical" href="https://acwy.fun/2023/04/10/xpath/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: true,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: true,
  }
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '1.2 xpath简单介绍',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-11-02 18:40:09'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
    win.getCSS = (url,id = false) => new Promise((resolve, reject) => {
      const link = document.createElement('link')
      link.rel = 'stylesheet'
      link.href = url
      if (id) link.id = id
      link.onerror = reject
      link.onload = link.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        link.onload = link.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(link)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 5.4.2"></head><body><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/pace-js/themes/blue/pace-theme-minimal.min.css"/><script src="https://cdn.jsdelivr.net/npm/pace-js/pace.min.js"></script><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src= "" data-lazy-src="/img/logo.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">28</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">35</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">0</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 目录</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友情链接</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg fixed" id="page-header" style="background-image: url('https://www.w3cschool.cn/attachments/image/20161103/1478161528585228.png')"><nav id="nav"><span id="blog-info"><a href="/" title="梧桐的学习日记"><span class="site-name">梧桐的学习日记</span></a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 目录</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友情链接</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">1.2 xpath简单介绍</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-04-10T07:54:23.000Z" title="发表于 2023-04-10 15:54:23">2023-04-10</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-11-02T10:40:09.707Z" title="更新于 2023-11-02 18:40:09">2023-11-02</time></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">814</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>3分钟</span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="xpath解析-最常用且便捷高效的一种解析方式"><a href="#xpath解析-最常用且便捷高效的一种解析方式" class="headerlink" title="xpath解析: 最常用且便捷高效的一种解析方式"></a>xpath解析: 最常用且便捷高效的一种解析方式</h2><blockquote>
<p>使用快捷方法：打开F12，右键点击html节点复制xpath</p>
</blockquote>
<h3 id="xpath解析原理"><a href="#xpath解析原理" class="headerlink" title="xpath解析原理:"></a>xpath解析原理:</h3><pre><code>1. 实例化一个etree对象,将要解析的页面元am加载到该对象中
2. 调用etree对象中的xpath方法,结合xpath表达式实现标签定位和内容捕获
</code></pre>
<h3 id="流程"><a href="#流程" class="headerlink" title="流程"></a>流程</h3><ol>
<li>安装 <code>pip install lxml</code></li>
<li>导入 <code>from lxml import etree</code></li>
<li>实例化etree对象:<ul>
<li>将本地的html文档中的源码加载到etree对象中:<br>  <code>tree = etree.parse(filePath)</code></li>
<li>将网络中获取的源码加载到etree中<br>  <code>tree = etree.HTML(&#39;page text&#39;)</code></li>
</ul>
</li>
</ol>
<h3 id="xpath表达式"><a href="#xpath表达式" class="headerlink" title="xpath表达式"></a>xpath表达式</h3><p><code>res = tree.xpath(xpath表达式)</code></p>
<ol>
<li><p><code>/</code>:表示从根节点开始定位,代表一个层级<br> 例,定位title: <code>/html/head/title</code></p>
</li>
<li><p><code>//</code>:表示多层级,可以从任意位置开始定位<br> 例,定位title: //title</p>
</li>
<li><p>属性定位: 根据一个标签的属性来定位,<code>tag[@attrName=&quot;attrValue&quot;]</code><br> 定位一个<code>带class=&#39;song&#39;的div</code><br> <code>//div[@class=&quot;song&quot;]</code> </p>
</li>
<li><p>索引定位: 多个标签,索引是从<code>1</code>开始的<br> 定位<code>指定div下的第三个p</code>: <code>//div[@clsss=&quot;song&quot;]/p[3]</code></p>
</li>
<li><p>取文本(返回的都是列表)</p>
<ul>
<li>/text()     # 取的是列表,直系文本</li>
<li><code>//text()</code>    # 标签中的非直系文本,所有的子标签文本</li>
</ul>
</li>
<li><p>取属性(返回的都是列表)<br> <code>/@src</code>     <code>/@attrName</code>    定位到标签,属性名前加<code>/@</code></p>
</li>
</ol>
<h3 id="实例1-58二手房-获取的标签循环后-定位问题"><a href="#实例1-58二手房-获取的标签循环后-定位问题" class="headerlink" title="实例1: (58二手房:获取的标签循环后,定位问题)"></a>实例1: (58二手房:获取的标签循环后,定位问题)</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> requests</span><br><span class="line">page_text = requests.get(url=url,headers=headers).text</span><br><span class="line">tree = etree.HTML(page_text, <span class="string">&#x27;lxml&#x27;</span>)</span><br><span class="line"><span class="comment"># 获取的是li的列表</span></span><br><span class="line">li_list = tree.xpath(<span class="string">&#x27;//ul@class=&quot;house-list-wrap&quot;/li&#x27;</span>)</span><br><span class="line">f = <span class="built_in">open</span>(<span class="string">&#x27;1.txt&#x27;</span>, <span class="string">&#x27;w&#x27;</span>)</span><br><span class="line"><span class="keyword">for</span> li <span class="keyword">in</span> li_list:</span><br><span class="line">	<span class="comment"># 循环每个li ./表示相对于li的层级</span></span><br><span class="line">	<span class="comment"># text()获取的是一个列表[&#x27;你好&#x27;]</span></span><br><span class="line">	title = li.xpath(<span class="string">&#x27;./div[2]/h2/a/text()&#x27;</span>)[<span class="number">0</span>]</span><br><span class="line">	f.write(title+<span class="string">&#x27;\n&#x27;</span>)</span><br></pre></td></tr></table></figure>

<h3 id="实例2-图片解析下载-中文乱码问题"><a href="#实例2-图片解析下载-中文乱码问题" class="headerlink" title="实例2: (图片解析下载: 中文乱码问题)"></a>实例2: (图片解析下载: 中文乱码问题)</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">url = <span class="string">&#x27;http://pic.netbian.com/4kmeinv&#x27;</span></span><br><span class="line">response = requests.get(url=url, headers=headers)</span><br><span class="line"><span class="comment"># 尝试手动设定响应数据编码,没有效果是有通用解决</span></span><br><span class="line"><span class="comment"># response.encoding = &#x27;utf-8&#x27;</span></span><br><span class="line">page_text = response.text</span><br><span class="line"><span class="comment"># 准备解析: src alt</span></span><br><span class="line">tree = etree.html(page_text, <span class="string">&#x27;lxml&#x27;</span>)</span><br><span class="line">li_list = tree.xpath(<span class="string">&#x27;//url[@class=&quot;slist&quot;]//li&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 准备文件夹</span></span><br><span class="line">dir_name = <span class="string">&#x27;./pic&#x27;</span></span><br><span class="line"><span class="keyword">if</span> <span class="keyword">not</span> os.path.exists(dir_name):</span><br><span class="line">	os.mkdir(dir_name)</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> li <span class="keyword">in</span> li_list:</span><br><span class="line">	img_src = li.xpath(<span class="string">&#x27;./a/img/@src&#x27;</span>)[<span class="number">0</span>]</span><br><span class="line">	img_src = <span class="string">&#x27;http://pic.netbian.com&#x27;</span>+img_src</span><br><span class="line">	<span class="comment"># 中文出现乱码</span></span><br><span class="line">	img_name = li.xpath(<span class="string">&#x27;./a/img/@alt&#x27;</span>)[<span class="number">0</span>] + <span class="string">&#x27;jpg&#x27;</span></span><br><span class="line">	<span class="comment"># 通用处理乱码解决方案</span></span><br><span class="line">	img_name = img_name.encode(<span class="string">&#x27;iso-8859-1&#x27;</span>).decode(<span class="string">&#x27;gbk&#x27;</span>)</span><br><span class="line">	<span class="comment"># print(img_name, img_src)</span></span><br><span class="line">	<span class="comment"># 请求图片</span></span><br><span class="line">	img_data = requests.get(url=img_src, headers=headers).content</span><br><span class="line">	img_path = dir_name+<span class="string">&#x27;/&#x27;</span>+img_name</span><br><span class="line">	<span class="keyword">with</span> <span class="built_in">open</span>(img_path, <span class="string">&#x27;wb&#x27;</span>) <span class="keyword">as</span> f:</span><br><span class="line">		f.write(img_data)</span><br><span class="line">		<span class="built_in">print</span>(img_name,<span class="string">&#x27;下载成功&#x27;</span>)</span><br></pre></td></tr></table></figure>

<h3 id="实例3-全国城市获取-热门城市-全部城市"><a href="#实例3-全国城市获取-热门城市-全部城市" class="headerlink" title="实例3: (全国城市获取:热门城市+全部城市)"></a>实例3: (全国城市获取:热门城市+全部城市)</h3><h4 id="分开解析"><a href="#分开解析" class="headerlink" title="分开解析"></a>分开解析</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">tree = etree.HTML(page_text)</span><br><span class="line"><span class="comment"># 热门城市</span></span><br><span class="line">host_li_list = tree.xpath(<span class="string">&#x27;//@div[@class=&quot;bottom&quot;]/ul/li&#x27;</span>)</span><br><span class="line">all_hot_city_names =[]</span><br><span class="line"><span class="keyword">for</span> li <span class="keyword">in</span> host_li_list:</span><br><span class="line">	hot_city_name = li.xpath(<span class="string">&#x27;./a/text()&#x27;</span>)[<span class="number">0</span>]</span><br><span class="line">	all_hot_city_names.append(hot_city_name)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 所有城市</span></span><br><span class="line">city_names_list = tree.xpath(<span class="string">&#x27;//div[@class=&quot;bottom&quot;]/ul/div[2]/li&#x27;</span>)</span><br><span class="line">all_city_names = []</span><br><span class="line"><span class="keyword">for</span> li <span class="keyword">in</span> city_names_list:</span><br><span class="line">	city_name = li.xpath(<span class="string">&#x27;./a/text()&#x27;</span>)[<span class="number">0</span>]</span><br><span class="line">	all_city_names.append(city_name)</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="同时解析-两者xpath不同"><a href="#同时解析-两者xpath不同" class="headerlink" title="同时解析,两者xpath不同"></a>同时解析,两者xpath不同</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#	//@div[@class=&quot;bottom&quot;]/ul/li/a</span></span><br><span class="line"><span class="comment"># //div[@class=&quot;bottom&quot;]/ul/div[2]/li/a</span></span><br><span class="line"><span class="comment"># 中间用 | 分开</span></span><br><span class="line">a_list = tree.xpath(<span class="string">&#x27;//@div[@class=&quot;bottom&quot;]/ul/li/a | //div[@class=&quot;bottom&quot;]/ul/div[2]/li/a&#x27;</span>)</span><br><span class="line">all_city_names = []</span><br><span class="line"><span class="keyword">for</span> li <span class="keyword">in</span> a_list:</span><br><span class="line">	city_name = li.xpath(<span class="string">&#x27;./a/text()&#x27;</span>)</span><br><span class="line">	all_city_names.append(city_name)</span><br><span class="line">	<span class="built_in">print</span>(all_city_names)</span><br></pre></td></tr></table></figure></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://acwy.fun">梧桐影下月光卧</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://acwy.fun/2023/04/10/xpath/">https://acwy.fun/2023/04/10/xpath/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://acwy.fun" target="_blank">梧桐的学习日记</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/python/">python</a><a class="post-meta__tags" href="/tags/%E8%87%AA%E5%8A%A8%E5%8C%96/">自动化</a><a class="post-meta__tags" href="/tags/%E7%88%AC%E8%99%AB/">爬虫</a><a class="post-meta__tags" href="/tags/%E7%BD%91%E9%A1%B5%E8%A7%A3%E6%9E%90/">网页解析</a><a class="post-meta__tags" href="/tags/xpath/">xpath</a></div><div class="post_share"><div class="social-share" data-image="https://www.w3cschool.cn/attachments/image/20161103/1478161528585228.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/img/wechatpay.png" target="_blank"><img class="post-qr-code-img" src= "" data-lazy-src="/img/wechatpay.png" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="/img/alipay.png" target="_blank"><img class="post-qr-code-img" src= "" data-lazy-src="/img/alipay.png" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2023/04/10/requests%20Code%20Cookie/" title="2. requests 验证码识别和Cookie"><img class="cover" src= "" data-lazy-src="https://www.cnlogo8.com/d/file/20210809/x33ajnaamczcnlogo8.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">2. requests 验证码识别和Cookie</div></div></a></div><div class="next-post pull-right"><a href="/2023/04/10/python%E6%89%93%E5%8C%85/" title="python pyinstaller打包"><img class="cover" src= "" data-lazy-src="https://img.php.cn/upload/article/000/000/034/5cdd0a4528883299.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">python pyinstaller打包</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2022/01/10/Beautifulsoup4/" title="1.1 BeautifulSoup简单介绍"><img class="cover" src= "" data-lazy-src="https://pic1.zhimg.com/v2-a4c6b2b3504284423e625419eef9760c_r.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-01-10</div><div class="title">1.1 BeautifulSoup简单介绍</div></div></a></div><div><a href="/2023/10/29/playwright%E8%87%AA%E5%8A%A8%E5%8C%962/" title="playwright自动化测试(2)"><img class="cover" src= "" data-lazy-src="https://upyun.acwy.fun/acwy.fun/playwright.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-10-29</div><div class="title">playwright自动化测试(2)</div></div></a></div><div><a href="/2023/04/10/requests%20Code%20Cookie/" title="2. requests 验证码识别和Cookie"><img class="cover" src= "" data-lazy-src="https://www.cnlogo8.com/d/file/20210809/x33ajnaamczcnlogo8.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-04-10</div><div class="title">2. requests 验证码识别和Cookie</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src= "" data-lazy-src="/img/logo.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">梧桐影下月光卧</div><div class="author-info__description">分享个人学习路上的小知识</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">28</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">35</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">0</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://beau.acwy.fun"><i class="fa-solid fa-envelope"></i><span>点击联系我</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content"><b> <font color="#e66b6d">欢</font> <font color="#e66d98">迎</font> <font color="#e66cc6">访</font> <font color="#cc6de6">问</font> <font color="#9770e6">a</font> <font color="#6d93e6">c</font> <font color="#6fcde6">w</font> <font color="#72e6b6">y</font> <font color="#72e689">的</font> <font color="#99e670">学</font> <font color="#cde670">习</font> <font color="#e6df72">日</font> <font color="#e6c073">记</font> </b> <p align="center"> <img src= "" data-lazy-src="https://upyun.acwy.fun/acwy.fun/公告1.gif" width="50" alt="mao"> </p> <p align="center">微信：acwy0718</p> <p align="center">邮箱：awy0718@gmail.com</p></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#xpath%E8%A7%A3%E6%9E%90-%E6%9C%80%E5%B8%B8%E7%94%A8%E4%B8%94%E4%BE%BF%E6%8D%B7%E9%AB%98%E6%95%88%E7%9A%84%E4%B8%80%E7%A7%8D%E8%A7%A3%E6%9E%90%E6%96%B9%E5%BC%8F"><span class="toc-number">1.</span> <span class="toc-text">xpath解析: 最常用且便捷高效的一种解析方式</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#xpath%E8%A7%A3%E6%9E%90%E5%8E%9F%E7%90%86"><span class="toc-number">1.1.</span> <span class="toc-text">xpath解析原理:</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B5%81%E7%A8%8B"><span class="toc-number">1.2.</span> <span class="toc-text">流程</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#xpath%E8%A1%A8%E8%BE%BE%E5%BC%8F"><span class="toc-number">1.3.</span> <span class="toc-text">xpath表达式</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9E%E4%BE%8B1-58%E4%BA%8C%E6%89%8B%E6%88%BF-%E8%8E%B7%E5%8F%96%E7%9A%84%E6%A0%87%E7%AD%BE%E5%BE%AA%E7%8E%AF%E5%90%8E-%E5%AE%9A%E4%BD%8D%E9%97%AE%E9%A2%98"><span class="toc-number">1.4.</span> <span class="toc-text">实例1: (58二手房:获取的标签循环后,定位问题)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9E%E4%BE%8B2-%E5%9B%BE%E7%89%87%E8%A7%A3%E6%9E%90%E4%B8%8B%E8%BD%BD-%E4%B8%AD%E6%96%87%E4%B9%B1%E7%A0%81%E9%97%AE%E9%A2%98"><span class="toc-number">1.5.</span> <span class="toc-text">实例2: (图片解析下载: 中文乱码问题)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9E%E4%BE%8B3-%E5%85%A8%E5%9B%BD%E5%9F%8E%E5%B8%82%E8%8E%B7%E5%8F%96-%E7%83%AD%E9%97%A8%E5%9F%8E%E5%B8%82-%E5%85%A8%E9%83%A8%E5%9F%8E%E5%B8%82"><span class="toc-number">1.6.</span> <span class="toc-text">实例3: (全国城市获取:热门城市+全部城市)</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%88%86%E5%BC%80%E8%A7%A3%E6%9E%90"><span class="toc-number">1.6.1.</span> <span class="toc-text">分开解析</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%90%8C%E6%97%B6%E8%A7%A3%E6%9E%90-%E4%B8%A4%E8%80%85xpath%E4%B8%8D%E5%90%8C"><span class="toc-number">1.6.2.</span> <span class="toc-text">同时解析,两者xpath不同</span></a></li></ol></li></ol></li></ol></div></div></div></div></main><footer id="footer" style="background-image: url('https://www.w3cschool.cn/attachments/image/20161103/1478161528585228.png')"><div id="footer-wrap"><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text"><div> <a href="https://beian.miit.gov.cn/" target="_blank">豫ICP备2023010490号-1</a> </div> <div> <a style="display:flex;justify-content:center;align-items:center;" target="_blank" href="http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=41152402000204"> <img src= "" data-lazy-src="/img/beian.png" style="width:20px;height:20px;margin:0 5px"> <span>豫公网安备 41152402000204号</span> </a> </div> <div> <a style="display:flex;justify-content:center;" href="https://www.upyun.com/?utm_source=lianmeng&utm_medium=referral" target="_blank"> <span>本网站由</span> <img style="height:30px;margin:0 3px" src= "" data-lazy-src="/img/upyun_logo5.png"> <span>提供CDN加速/云存储服务</span> </a> <div/></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload/dist/lazyload.iife.min.js"></script><div class="js-pjax"></div><canvas class="fireworks" mobile="false"></canvas><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/fireworks.min.js"></script><script id="canvas_nest" defer="defer" color="0,0,255" opacity="0.7" zIndex="-1" count="99" mobile="true" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-nest.min.js"></script></div></body></html>